//
//  SettingViewController.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/3.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "SettingViewController.h"
#import <MessageUI/MessageUI.h>
#import "UITableViewController+TableViewSeparator.h"
#import "LanguageTableViewController.h"
#import "UserInfoTableViewController.h"
@interface SettingViewController ()<MFMailComposeViewControllerDelegate>

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    //依照螢幕寬度來設定Cell 高度
    return CGRectGetWidth([UIScreen mainScreen].bounds)*0.1375;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //依照螢幕寬度來設定Cell 高度
    return CGRectGetWidth([UIScreen mainScreen].bounds)*0.1375;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    // Return the number of sections.
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    }else if (section == 1)
    {
        return 2;
    }else if (section == 2)
    {
        return 1;
    }else if (section == 3)
    {
        return 2;
    }else
    {
        return 0;
    }
    // Return the number of rows in the section.
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //由于此方法调用十分频繁，cell的标示声明成静态变量有利于性能优化
    static NSString *cellIdentifier=@"cell";
    static NSString *cellIdentifierForTwoRow=@"twoCell";
    
    UITableViewCell *cell;
    
    if (indexPath.section==1 && indexPath.row == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierForTwoRow forIndexPath:indexPath];
        UISwitch *sw=[[UISwitch alloc]init];
        [sw addTarget:self action:@selector(switchValueChange:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView=sw;
        
    }else
    {
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
        
    }
    
    if (indexPath.section == 0) {
        cell.textLabel.text = @"個人資料";
    }else if (indexPath.section == 1)
    {
        switch (indexPath.row) {
            case 0:
                
                cell.textLabel.text = @"提醒";
                break;
            case 1:
                cell.textLabel.text = @"勿擾模式";
                
                break;
            default:
                break;
        }
        // Configure the cell...
    }else if (indexPath.section == 2)
    {
        cell.textLabel.text = @"語言";
        
    }else if (indexPath.section == 3)
    {
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = @"意見回饋";
                
                break;
            case 1:
                cell.textLabel.text = @"關於";
                
                break;
            default:
                break;
        }
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        //cell.textLabel.text = @"個人資料";
        //使用者資料
        UserInfoTableViewController *userInfoVC;
        userInfoVC = [self.storyboard instantiateViewControllerWithIdentifier:@"UserInfoTableViewController"];
        [self.navigationController pushViewController:userInfoVC animated:YES];

    }else if (indexPath.section == 1)
    {
        switch (indexPath.row) {
            case 0:
                //cell.textLabel.text = @"提醒";
                
                break;
            case 1:
                //cell.textLabel.text = @"勿擾模式";
                
                break;
            default:
                break;
        }
        // Configure the cell...
    }else if (indexPath.section == 2)
    {
        //cell.textLabel.text = @"語言";
        LanguageTableViewController *LanguageTableVC = [self.storyboard instantiateViewControllerWithIdentifier:@"LanguageTableViewController"];
        [self.navigationController pushViewController:LanguageTableVC animated:YES];
        
    }else if (indexPath.section == 3)
    {
        switch (indexPath.row) {
            case 0:
                //cell.textLabel.text = @"意見回饋";
                [self showEmail:@""];

                break;
            case 1:
                //cell.textLabel.text = @"關於";
                
                break;
            default:
                break;
        }
    }


}
#pragma mark 切换开关转化事件
-(void)switchValueChange:(UISwitch *)sw{
    
    
}

- (void)showEmail:(NSString*)webString {
    
    NSArray *toRecipents = [NSArray arrayWithObject:@"service@mooredoll.com"];
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:@"意見回饋"];
    [mc setToRecipients:toRecipents];
    
    //以下是附加檔案
    /*
     // Determine the file name and extension
     NSArray *filepart = [file componentsSeparatedByString:@"."];
     NSString *filename = [filepart objectAtIndex:0];
     NSString *extension = [filepart objectAtIndex:1];
     
     // Get the resource path and read the file using NSData
     NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
     NSData *fileData = [NSData dataWithContentsOfFile:filePath];
     
     // Determine the MIME type
     NSString *mimeType;
     if ([extension isEqualToString:@"jpg"]) {
     mimeType = @"image/jpeg";
     } else if ([extension isEqualToString:@"png"]) {
     mimeType = @"image/png";
     } else if ([extension isEqualToString:@"doc"]) {
     mimeType = @"application/msword";
     } else if ([extension isEqualToString:@"ppt"]) {
     mimeType = @"application/vnd.ms-powerpoint";
     } else if ([extension isEqualToString:@"html"]) {
     mimeType = @"text/html";
     } else if ([extension isEqualToString:@"pdf"]) {
     mimeType = @"application/pdf";
     }
     
     // Add attachment
     [mc addAttachmentData:fileData mimeType:mimeType fileName:filename];
     */
    // Present mail view controller on screen
    [self presentViewController:mc animated:YES completion:NULL];
    
}


- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }
    
    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
